do "E:/ReplicateBuild/02_code/00_environment/00_set_environment.do"


use "$basedata/ncerdc_experience.dta", clear
egen minyr=min(sy), by(ncerdc_id)
gen t=sy
keep minyr ncerdc_id t
save "$temp/minyr", replace
*match VA and experience data 
use "$basedata/va_estimates_drift", clear
keep if s!=.
gen sy =t
gen ncerdc_id=j
gen ncerdc_lea =real(lea)
gen ncerdc_schlcode=real(schlcode)
merge m:1 ncerdc_id sy ncerdc_lea ncerdc_schlcode using "$basedata/ncerdc_experience.dta"
ren _m mrgEXP
ren s SS
egen s=group(ncerdc_lea ncerdc_schlcode) 
keep if s!=.
bys j t: egen num_schools_t = count(s)
*deal with multiple schools
tab num_schools_t
sort j s t
gen lead_num_schl=(num_schools_t[_n+1]>1 & num_schools_t<=1)
gen temp=(num_schools_t>1 & j[_n-1]==j & s==s[_n-1] & t-1==t[_n-1])
egen temp2=max(temp), by(j t)
sort j 
drop if num_schools_t>1 & temp!=1 & temp2==1
bys j t: egen nst = count(s)
tab nst
duplicates drop j t, force
drop temp*
gen temp=(mu_jt_m1_hat_preY_ma!=. &  mu_jt_m2_hat_preY_ma!=. )
egen va_tchr=max(temp), by(j)

by j: egen num_schools_j=count(s) if temp==1
drop temp

sort j t s

xtset j t

egen lea_code = group(lea)

qui summ lea_code if lea=="XXX" /// hide identity
local FOCAL = r(mean)
*generate transfers
egen miny=min(sy), by(j)
gen transfer = s!=L.s if j==L.j & t==L.t+1
replace transfer=1 if s!=L2.s & j==L2.j & t==L2.t+2 & L.transfer!=1
*build in those who applied to transfer but didn't
preserve 
 use "$basedata/FOCAL_applications.dta", clear

merge m:1 applicant_id  using "$basedata/FOCAL_applicants_crosswalk"
ren _m mrgFOCAL1
gen j=ncerdc_id
gen t= app_year
merge m:1 t ncerdc_id using "$temp/minyr"
ren _m mrgMYR
drop if j==. | school_code==.
egen app_count=sum(1) if t>=minyr, by(j )
collapse app_count, by(j )
save "$temp/tempAPP.dta", replace 
restore
merge m:1 j  using "$temp/tempAPP.dta"
replace transfer=1 if app_count>0 & app_count!=.
replace transfer=1 if lead_num_schl==1

gen temp1=(app_count>0 & app_count!=.)
egen temp2=max(temp1), by(j)
*focus on those we see total career until present
gen temp=(lea=="XXX" & sy==miny) /// hide identity
egen FOCALstart=max(temp), by(j)
drop temp
gen temp=(lea=="XXX") /// hide identity
egen everFOCAL=max(temp), by(j)
replace everFOCAL=1 if temp2==1
egen min_exp=min( tchr_exp_pay_level) ,by(j)
egen trans_max=max(transfer), by(ncerdc_id)

drop temp*
cap gen abs_adv=(mu_jt_m1_hat_preY_ma*(p_m1_ma/(p_m1_ma+p_m2_ma)))+  (mu_jt_m2_hat_preY_ma*(p_m2_ma/(p_m1_ma+p_m2_ma)))
cap gen comp_adv= mu_jt_m2_hat_preY_ma-abs_adv
cap egen pickone=tag(j) 
clear matrix
foreach i in abs_adv comp_adv {
	
	foreach  v in 0 {
sum `i' if FOCALstart==1 & va_tchr==1 & miny>2009 & trans_max==`v'
mat a`i' =(r(mean)) 
mat a`i'=(a`i',r(sd))
reg `i' if FOCALstart==1 & va_tchr==1 & miny>2009 & trans_max==`v', cluster(ncerdc_id)
mat a`i'=(a`i', e(N_clust) )
}
	foreach  v in 1 {
sum `i' if FOCALstart==1 & va_tchr==1 & miny>2009 & trans_max==`v'
mat a`i' =(a`i',r(mean)) 
mat a`i'=(a`i',r(sd))
reg `i' if FOCALstart==1 & va_tchr==1 & miny>2009 & trans_max==`v', cluster(ncerdc_id)
mat a`i'=(a`i', e(N_clust) )
}
}
matrix rownames acomp_adv ="Comparative advantage"
matrix rownames aabs_adv ="Absolute advantage"

matrix j= acomp_adv\aabs_adv
frmttable using "$tables/tranfer_samp", statmat(j) replace va tex fra ///
	ctitles("", "\multicolumn{3}{c}{{Did not transfer}}", "\multicolumn{3}{c}{{Applied to transfer}}" \"","mean","sd","count","mean","sd","count") ///
	sdec(4,4,0,4,4,0 \ 4,4,0,4,4,0)
